﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DAL;
using SimpComm;

namespace Test
{
    /// <summary>
    /// 数据库测试 的摘要说明
    /// </summary>
    [TestClass]
    public class 数据库测试
    {


        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void 选择()
        {
            using (PizzaDataDataContext db = new PizzaDataDataContext())
            {
                var data = db.AdmCol.FirstOrDefault(p => p.ID == 1);
                Assert.IsTrue(data != null && data.ID > 0);
            }
        }
        [TestMethod]
        public void 多选选择()
        {
            using (PizzaDataDataContext db = new PizzaDataDataContext())
            {
                var count = db.AdmCol.Count(p => p.ID > 4);
                Assert.IsTrue(count > 0);
            }
        }
        [TestMethod]
        public void 动态排序()
        {
            using (PizzaDataDataContext db = new PizzaDataDataContext())
            {
                var select = db.AdmCol.Select(p => p);
                var data = select.OrderBy(DynamicOrderby.Creat("ClassLevel", "desc", "ID", "desc").ToString()).FirstOrDefault();
                var datas1 = (from p in db.AdmCol
                              orderby p.ClassLevel descending, p.ID descending
                              select p).FirstOrDefault();
                Assert.IsTrue(data.ID == datas1.ID);


            }
        }
        [TestMethod]
        public void 修改()
        {
            using (PizzaDataDataContext db = new PizzaDataDataContext())
            {
                var data = db.AdmCol.FirstOrDefault(p => p.ID == 3);
                var temp = data.Name = data.Name + "1";
                db.SubmitChanges();
                var data1 = db.AdmCol.FirstOrDefault(p => p.ID == 3);
                Assert.IsTrue(data1.Name == temp);
            }
        }
        [TestMethod]
        public void 批量修改()
        {
            using (PizzaDataDataContext db = new PizzaDataDataContext())
            {
                var rows = db.AdmCol.UpdateBatch(p => p.ID > 5, p => new AdmCol() { Descript = "yep", AdmColID = 9 });
                Assert.IsTrue(rows > 0);
            }
        }
        [TestMethod]
        public void 删除()
        {
            using (PizzaDataDataContext db = new PizzaDataDataContext())
            {

            }
        }
    }
}
